package com.yay.rental.ui;

import java.lang.ref.SoftReference;
import com.yay.rental.R;
import com.yay.rental.provider.RentalContract.Category;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.HorizontalScrollView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


/**
 * Get Started!
 * @author team@getstarted.com.ua
 * 
 * Front-door {@link Activity} 
 */
public class HomeActivity extends Activity {
    public static String SERVER_URL;
    private boolean listMode;
	private CharSequence[] bd_items; 
    private CharSequence[] pt_items;
	private CharSequence[] bt_items;
	private CharSequence[] sl_items;
	private CharSequence[] th_items;
	private CharSequence[] req_items;
	private CharSequence[] ame_items;
	private CharSequence[] loc_items;
	private CharSequence[] act_items;
	private CharSequence[][] items; 
	private AlertDialog dlgFilter;
	
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        
    	bd_items = new CharSequence[] {getString(R.string.filter_bd_all), getString(R.string.filter_bd_studio), 
        		getString(R.string.filter_bd_1), getString(R.string.filter_bd_2), getString(R.string.filter_bd_3), getString(R.string.filter_bd_4)};

        pt_items = new CharSequence[] {getString(R.string.filter_pt_all), getString(R.string.filter_pt_apartment), 
        		getString(R.string.filter_pt_barn), getString(R.string.filter_pt_boat), getString(R.string.filter_pt_bungalow), getString(R.string.filter_pt_cabin),
        		getString(R.string.filter_pt_castle), getString(R.string.filter_pt_chalet), getString(R.string.filter_pt_condo), getString(R.string.filter_pt_cottage),
        		getString(R.string.filter_pt_estate), getString(R.string.filter_pt_farmhouse), getString(R.string.filter_pt_hotel), getString(R.string.filter_pt_house),
        		getString(R.string.filter_pt_lodge), getString(R.string.filter_pt_resort), getString(R.string.filter_pt_studio), getString(R.string.filter_pt_townhome),
        		getString(R.string.filter_pt_villa), getString(R.string.filter_pt_yacht)};

    	bt_items = new CharSequence[] {getString(R.string.filter_bt_all), getString(R.string.filter_bt_1), 
        		getString(R.string.filter_bt_2), getString(R.string.filter_bt_3), getString(R.string.filter_bt_4)};

    	sl_items = new CharSequence[] {getString(R.string.filter_sl_all), getString(R.string.filter_sl_1), 
        		getString(R.string.filter_sl_5), getString(R.string.filter_sl_9), getString(R.string.filter_sl_13)};

    	th_items = new CharSequence[] {getString(R.string.filter_th_all), getString(R.string.filter_th_adv), 
        		getString(R.string.filter_th_budget), getString(R.string.filter_th_romantic), getString(R.string.filter_th_tourist), getString(R.string.filter_th_away),
        		getString(R.string.filter_th_family), getString(R.string.filter_th_luxury), getString(R.string.filter_th_activity), getString(R.string.filter_th_spa),
        		getString(R.string.filter_th_historic), getString(R.string.filter_th_farm), getString(R.string.filter_th_holiday)};

    	req_items = new CharSequence[] {getString(R.string.filter_req_children), getString(R.string.filter_req_pet), 
        		getString(R.string.filter_req_wheelchair), getString(R.string.filter_req_nonsm), getString(R.string.filter_req_suitable), getString(R.string.filter_req_long)};
    	
    	ame_items = new CharSequence[] {getString(R.string.filter_ame_air), getString(R.string.filter_ame_int), 
        		getString(R.string.filter_ame_sat), getString(R.string.filter_ame_dish), getString(R.string.filter_ame_pool), getString(R.string.filter_ame_wash)};

    	loc_items = new CharSequence[] {getString(R.string.filter_loc_beach), getString(R.string.filter_loc_lake), 
        		getString(R.string.filter_loc_waterfront), getString(R.string.filter_loc_mountain), getString(R.string.filter_loc_ocean), getString(R.string.filter_loc_resort),
        		getString(R.string.filter_loc_rural), getString(R.string.filter_loc_town), getString(R.string.filter_loc_village), getString(R.string.filter_loc_river),
        		getString(R.string.filter_loc_downtown)};

    	act_items = new CharSequence[] {getString(R.string.filter_act_golf), getString(R.string.filter_act_skiing), 
        		getString(R.string.filter_act_fishing)};
	
    	items = new CharSequence[][] {bd_items, pt_items, bt_items, sl_items, th_items, req_items, ame_items, loc_items, act_items}; 

    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        dlgFilter = null;
    }
    
    @Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.menu, menu);
        return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
		    case R.id.bookmarks:
		    	return true;
		    case R.id.inquiries:
		    	return true;
		    default: finish();
		    	return true;
	    }
	}
	
    /** Handle "show map" title-bar action. */
    public void onShowMapClick(View v) {
    	if(!listMode) {
    		SoftReference<Drawable> icList = RentalApplication.getInstance().getDrawableHashMap().get("ic_title_show_list");
    		if(icList == null) {
    			icList = new SoftReference<Drawable>(getResources().getDrawable(R.drawable.ic_title_show_list));   
    			RentalApplication.getInstance().getDrawableHashMap().put("ic_title_show_list", icList);   
    		}
    		((ImageButton) v).setImageDrawable(icList.get());
    		listMode = true;
    	} else {
    		SoftReference<Drawable> icMap = RentalApplication.getInstance().getDrawableHashMap().get("ic_title_show_map");
    		if(icMap == null) {
    			icMap = new SoftReference<Drawable>(getResources().getDrawable(R.drawable.ic_title_show_map));   
    			RentalApplication.getInstance().getDrawableHashMap().put("ic_title_show_map", icMap);   
    		}
    		((ImageButton) v).setImageDrawable(icMap.get());
    		listMode = false;
    	}
    }

    /** Handle "sort" title-bar action. */
    public void onSortClick(View v) {
        final CharSequence[] items = {getString(R.string.sort_lastUpdated), getString(R.string.sort_numOfBedrooms), 
        		getString(R.string.sort_price), getString(R.string.sort_numOfReviews), getString(R.string.sort_avgRating), getString(R.string.sort_distance)};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getString(R.string.description_sort));
        builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
				Intent intent = new Intent(Intent.ACTION_VIEW, Category.buildCategoryUri("0"));
		        startActivity(intent);
                dialogInterface.dismiss();
            }
        });
        builder.create().show(); 
    }

    /** Handle "search" title-bar action. */
    public void onSearchClick(View v) {
    	Intent intent = new Intent(Intent.ACTION_VIEW, Category.buildCategoryUri("0"));
        startActivity(intent);
    }
    
    /** Handle "filter" title-bar action. */
    public void onFilterClick(View v) {
    	buildDialog(0);
    }
    
    private void buildDialog(int position) {
    	final AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LinearLayout title = new LinearLayout(this);
        title.setOrientation(LinearLayout.VERTICAL);
        title.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        builder.setCustomTitle(title);

        final TextView hdr = new TextView(this);
        hdr.setText(getString(Filter.values()[position].getName()));
        hdr.setTextColor(getResources().getColor(R.color.white));
        hdr.setPadding(10,10,10,10);
        title.addView(hdr);
        
        HorizontalScrollView scroll = new HorizontalScrollView(this);
        scroll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        scroll.setHorizontalScrollBarEnabled(false);
        LinearLayout icons = new LinearLayout(this);
        icons.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        icons.setBackgroundColor(this.getResources().getColor(R.color.title_background));
        for(int i=0; i< Filter.values().length; i++){
        	final Filter item = Filter.values()[i];
        	ImageView img = new ImageView(this);
	    	img.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	        img.setImageResource(item.getDrawable());
	        img.setScaleType(ImageView.ScaleType.FIT_XY);
	        img.setPadding(5,0,5,0);
	        img.setOnClickListener(new OnClickListener() {
	        	@Override
				public void onClick(View arg0) {
					dlgFilter.dismiss();
	        		buildDialog(item.getId());
				}
	        });
	        icons.addView(img);
	        
	        ImageView div = new ImageView(this);
	        div.setLayoutParams(new LayoutParams(1, LayoutParams.FILL_PARENT));
	        div.setImageDrawable(this.getResources().getDrawable(R.drawable.list_background));
	        icons.addView(div);
        }
        scroll.addView(icons);
        title.addView(scroll);
        
        builder.setSingleChoiceItems(items[position], 0, new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
				Intent intent = new Intent(Intent.ACTION_VIEW, Category.buildCategoryUri("0"));
		        startActivity(intent);
                dialogInterface.dismiss();
            }
        });
        builder.setPositiveButton(getString(R.string.btn_ok), new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
            	dialogInterface.dismiss();
            }
        });
        builder.setNeutralButton(getString(R.string.btn_clearAll), new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
            	
            }
        });
        builder.setNegativeButton(getString(R.string.btn_cancel), new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialogInterface, int item) {
            	dialogInterface.dismiss();
            }
        });
        dlgFilter = builder.create();
        dlgFilter.show();
    }
    
	enum Filter {
		btn_filter_bedrooms(0, R.string.btn_filter_bedrooms, R.drawable.btn_filter_bedrooms),
		btn_filter_property_type(1, R.string.btn_filter_property_type, R.drawable.btn_filter_property_type),
		btn_filter_bathrooms(2, R.string.btn_filter_bathrooms, R.drawable.btn_filter_bathrooms),
		btn_filter_btn_filter_sleepsbedrooms(3, R.string.btn_filter_sleeps, R.drawable.btn_filter_sleeps),
		btn_filter_theme(4, R.string.btn_filter_theme, R.drawable.btn_filter_theme),
		btn_filter_special_requirments(5, R.string.btn_filter_special_requirments, R.drawable.btn_filter_special_requirments),
		btn_filter_amenities(6, R.string.btn_filter_amenities, R.drawable.btn_filter_amenities),
		btn_filter_location_type(7, R.string.btn_filter_location_type, R.drawable.btn_filter_location_type),
		btn_filter_activities(8, R.string.btn_filter_activities, R.drawable.btn_filter_activities);
		
		private int id;
		private int name;
		private int drawable;
		
		private Filter(int id, int name, int drawable) {
			this.id = id;
			this.name = name;
			this.drawable = drawable;
		}
		
		public int getId() {
			return id;
		}

		public int getName() {
			return name;
		}

		public int getDrawable() {
			return drawable;
		}
	}
	
}
